<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <script src = "js/jquery.min.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo.jpg" alt="">
        <span class="title">Little Li ' s Blog</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="index.html">主页</a>
        <a href="blog_edit.html">写博客</a>
        <a href="login.html">登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的登陆框 -->
        <div class="login-dialog">
            <h3>注册</h3>
            <div class="row">
                <span>昵称</span>
                <input type="text" id="nickname">
            </div>
            <div class="row">
                <span>登录名</span>
                <input type="text" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" id="password">
            </div>
            <div class="row">
                <span>确认密码</span>
                <input type="password" id="password2">
            </div>
            <div class="row">
                <button id="submit" onclick="mySubmit()">提交</button>
            </div>
        </div>
    </div>

    <script>
        //注册的数据提交（ajax）
        function mySubmit(){
            //非空效验和正确性效验
            //#对应的是id
            var nickname = jQuery("#nickname");
            var username = jQuery("#username");
            var password = jQuery("#password");
            var password2 = jQuery("#password2");

            if(jQuery.trim(nickname.val()) == ""){
                alert("抱歉请先输入昵称");
                nickname.focus();
                return;
            }
            if(jQuery.trim(username.val()) == ""){
                alert("抱歉请先输入登录名");
                username.focus();
                return;
            }
            if(jQuery.trim(password.val()) == ""){
                alert("抱歉请先输入密码");
                password.focus();
                return;
            }

            //密码强度效验(密码的长度必须大于8)
            if(jQuery.trim(password.val()).length < 8){
                alert("密码强度太低，请设置至少8位的密码！");
                password.focus();
                return;
            }

            if(jQuery.trim(password2.val()) != jQuery.trim(password.val())){
                alert("抱歉，两次输入的密码不一致，请重新输入");
                password.focus();
                return;
            }

            //发送ajax请求到后端
            jQuery.ajax({
                url:"reg",
                type:"POST",
                data:{
                    "username":username.val(),
                    "nickname":nickname.val(),
                    "password":password.val()
                },
                success:function(data){
                    if(data != null && data.succ == 200){
                        if(data.data == 1){
                            //注册成功
                            alert("恭喜，注册成功！");
                            //跳转到登录页面
                            location.href = "login.html";
                        }
                        else{
                            //注册失败
                            alert("很抱歉，注册失败！" + data.msg);
                        }
                    }
                    else{
                        alert("服务器繁忙！")
                    }
                }
            });
        }
    </script>
</body>

</html>